package com.mj.guoqi.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.SYY.cbsdk.ui.AppControl;
import com.dyk.hfsdk.dao.util.DevListener;
import com.dyk.hfsdk.ui.Access;
import com.mj.guoqi.R;
import com.mj.guoqi.adapter.GuoqiAdapter;
import com.mj.guoqi.bean.GuoqiDto;
import com.mj.guoqi.bean.GuoqiList;
import com.mj.guoqi.service.GuoqiService;
import com.mj.guoqi.swipeback.SwipeBackActivity;
import com.mj.guoqi.util.DateUtils;
import com.mj.guoqi.widget.LoadingDialog;
import com.mj.guoqi.widget.XListView;
import com.mj.guoqi.widget.XListView.IXListViewListener;

/**
 * 国旗列表
 * @author zhaominglei
 * @date 2015-8-15
 * 
 */
public class GuoqiListActivity extends SwipeBackActivity implements OnClickListener,IXListViewListener,DevListener {
	@SuppressWarnings("unused")
	private static final String TAG = GuoqiListActivity.class.getSimpleName();
	private GuoqiService guoqiService = new GuoqiService();
	private ImageView goHome;
	private LinearLayout miniAdLinearLayout; //迷你广告
	private LoadingDialog loadingDialog; //加载框
	private XListView guoqiListView;
	private List<GuoqiDto> guoqiDtos = new ArrayList<GuoqiDto>();
	private	GuoqiAdapter guoqiAdapter;
	private int page = 1; //页码
	private String referer; //来自那个链接
	private String keyword; //关键词
	private Button appOffersButton; //推荐应用
	private Access access;
	private AppControl appControl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_guoqi_list);
		init();
	}
	
	private void init() {
		goHome = (ImageView)findViewById(R.id.guoqilist_gohome);
		miniAdLinearLayout = (LinearLayout)findViewById(R.id.guoqilist_miniAdLinearLayout);
		guoqiListView = (XListView)findViewById(R.id.guoqilist_listview);
		appOffersButton = (Button)findViewById(R.id.guoqilist_appOffersButton);
		guoqiAdapter = new GuoqiAdapter(this, guoqiDtos);
		guoqiListView.setPullLoadEnable(true);
		guoqiListView.setXListViewListener(this);
		guoqiListView.setAdapter(guoqiAdapter);
		
		goHome.setOnClickListener(this);
		appOffersButton.setOnClickListener(this);
		loadingDialog = new LoadingDialog(this);
		
		appControl = AppControl.getInstance();
		appControl.init(GuoqiListActivity.this, MainActivity.MUMAYI_APPID, MainActivity.MUMAYI_APPCHANNEL);
		appControl.loadPopAd(GuoqiListActivity.this);
		appControl.showPopAd(GuoqiListActivity.this, 60 * 1000);
		appControl.showInter(GuoqiListActivity.this, miniAdLinearLayout);
		
		access = Access.getInstance();
		// 初始化带player_id
		access.init(GuoqiListActivity.this, MainActivity.MUMAYI_APPID, MainActivity.MUMAYI_APPCHANNEL);
		// 设置初始积分                                                              
		access.setdefaultSCORE(this, 100);
		// 设置获取积分的监听
		access.setAppListener(this, this);
		
		referer = getIntent().getStringExtra("referer");
		keyword = getIntent().getStringExtra("keyword");
		if ((keyword == null || keyword.equals("")) && (referer == null || referer.equals(""))) {
			Toast.makeText(getApplicationContext(), R.string.guoqi_error1, Toast.LENGTH_SHORT).show();
			GuoqiListActivity.this.finish();
		}
		getGuoqiList();
	}
	
	private void getGuoqiList() {
		loadingDialog.show();
		new GuoqiListAsyncTask().execute("");
	}
	
	@Override
	public void onRefresh() {
		getGuoqiList();
	}

	@Override
	public void onLoadMore() {
		getGuoqiList();
		page++;
	}
	
	//刷新结束
	public void onLoad() {
		guoqiListView.stopRefresh();
		guoqiListView.stopLoadMore();
		guoqiListView.setRefreshTime(DateUtils.format(new Date(), DateUtils.datePattern));
	}	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.guoqilist_gohome:
			GuoqiListActivity.this.finish();
			break;
		
		case R.id.guoqilist_appOffersButton:
			access.openWALL(GuoqiListActivity.this);
			break;
			
		default:
			break;
		}
	}
	
	@Override
	public void onBackPressed() {
		GuoqiListActivity.this.finish();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		appControl.close(this);
	}
	
	public class GuoqiListAsyncTask extends AsyncTask<String, String, GuoqiList> {
		@Override
		protected GuoqiList doInBackground(String... params) {
			return guoqiService.getGuoqiList(getApplicationContext(), referer, keyword, page);
		}
		@Override
		protected void onPostExecute(GuoqiList result) {
			loadingDialog.dismiss();
			if (result != null && result.getRecordList() != null && result.getRecordList().size() > 0) {
				guoqiAdapter.setGuoqiDtos(result.getRecordList());
				guoqiAdapter.notifyDataSetChanged();
				onLoad();
			}
		}
	}
	
	@Override
	public void onDevSucceed(int eggs) {
		System.out.println("积分获取成功:" + eggs);
	}

	@Override
	public void onDevFailed(String message) {
		System.out.println("积分获取失败:" + message);
	}

	@Override
	public void onClose(Context context) {
		((Activity) context).finish();
	}		
}
